import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:hzy_normal_tool/hzy_normal_tool.dart';

import '../../utils/index.dart';
import '../../widgets/index.dart';
import 'base_state.dart';

abstract class BaseListState<T extends StatefulWidget> extends BaseStateV<T>
    with HzyAbstracRefreshWidget, HzyAbstracRefreshMehod {
  /// 刷新控制器
  EasyRefreshController? refreshController;

  /// 配置下啦刷新
  bool controlFinishRefresh = true;

  /// 配置上啦加载
  bool controlFinishLoad = true;

  @override
  @mustCallSuper
  configDefault() {
    configRefreshController();
  }

  /// =============== UI配置 =============== ///
  ///
  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    return createRefreshWidget(
      context,
    );
  }

  @override
  Widget createRefreshWidget(
    BuildContext context,
  ) {
    Widget body = EasyRefresh(
      controller: refreshController,
      onRefresh: () async {
        configRefresh();
      },
      onLoad: () async {
        configLoading();
      },
      header: createHeader(),
      footer: createFooter(),
      child: createRefreshChild(context),
    );
    return body;
  }

  /// 创建刷新子试图
  createRefreshChild(
    BuildContext context,
  ) {
    Widget body = createListView(
      context,
    );
    return body;
  }

  /// 配置刷新header
  @override
  Header? createHeader() {
    return CommonRefreshWidget.configHeader();
  }

  /// 配置刷新footer
  @override
  Footer? createFooter() {
    return CommonRefreshWidget.configFooter();
  }

  @override
  Widget createListView(
    BuildContext context,
  ) {
    throw UnimplementedError();
  }

  @override
  Widget createListitem(
    BuildContext context,
    int index,
  ) {
    throw UnimplementedError();
  }

  /// =============== 网络事件 =============== ///

  /// =============== 触发事件 =============== ///

  // 点击缺省页触发事件
  @override
  tapPlaceHoldWidgetMethod({
    required CommonPlaceHoldType placeHoldType,
  }) {}

  // 上啦加载触发事件
  @override
  void configLoading() {
    page++;
    getNetWorkData(
      type: 2,
    );
  }

  // 下啦刷新触发事件
  @override
  void configRefresh() {
    page = 1;
    getNetWorkData(
      type: 1,
    );
  }

  /// 创建刷新控制器
  @override
  void configRefreshController() {
    refreshController = EasyRefreshController(
      controlFinishLoad: controlFinishLoad,
      controlFinishRefresh: controlFinishRefresh,
    );
  }

  /// 执行结束刷新
  /// [type]  1  下拉刷新  2 上拉加载  3  手动刷新
  @override
  PageState endRefresh({
    required int type,
    required PageState state,
  }) {
    PageState normalState = configEndRefresh(
      type: type,
      state: state,
      refreshController: refreshController,
    );
    if (type == 1 || type == 3) {
      configSetState(() {
        pageState = normalState;
      });
    }
    return normalState;
  }
}
